<template>

  <div class="login_container">
    <div class="box_one"></div>
    <div class="box_two"></div>
    <div class="box_three"></div>
    <div class="box_four"></div>
    <div class="login_box">
      <!--表单提交区域-->
      <el-form :rules="loginFormRules" ref="loginFormRef" label-width="0px" class="login_form" :model="loginForm">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user" placeholder="请输入账号"
            clearable></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-3702mima"
            placeholder="请输入密码" show-password clearable></el-input>
        </el-form-item>
        <!--按钮区-->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="register">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
  import axios from 'axios';
  export default {
    data() {
      return {

        loginForm: {
          // 登录的表单数据的绑定对象
          id: '',
          username: '',
          telphone: '',
          password: '',
          isadmin: ''

        },
        loginFormRules: {
          // 验证用户名是否合法
          username: [
            { required: true, message: '请输入登录名称', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          // 验证密码是否合法
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' },
            { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    mounted() {
    },
    methods: {
      resetLoginForm() { // 点击重置按钮,重置登录表单
        this.$refs.loginFormRef.resetFields()
      },
      login() { // 点击登录按钮，跳转到indexPage.vue

        let loginDate = {
          "username": "",
          "password": "",
        }

        loginDate.username = this.loginForm.username;
        loginDate.password = this.loginForm.password;

        axios.post("http://localhost:8080/loginVer", loginDate).then((result) => {

          if (result.data.code == 0) {
            //登录失败
            this.$message.error("登录失败，请检查用户名或密码");
          }
          else {
            //登录成功
            this.loginForm = result.data.data;
            this.$message.success("登录成功，欢迎您！");
            if (result.data.data.isadmin == 1) {
              this.$router.push({
                path: '/indexPage',
                query:{
                  loginUserId:this.loginForm.id
                }
              }) // 跳转到indexPage.vue
            } else {
              // 跳转到前端的主界面 需要修改
              this.$router.push({
                path: '/serviceIndex',
                query:{
                  loginUserId:this.loginForm.id
                }
              })
            }
          }
        })




      },
      register() { // 跳转到注册界面
        this.$router.push('/register')
      }
    }
  }
</script>
<style lang="less" scoped>
  .login_container {
    position: absolute;

    left: 16%;
    height: 700px;
    background-color: #384b5f;
    width: 1000px;
  }

  .box_one {
    position: absolute;
    left: 89%;
    top: 5%;
    height: 200px;
    width: 200px;
    background-color: #ffcc99c4;
  }

  .box_two {
    position: absolute;
    left: 95%;
    top: 75%;
    height: 200px;
    width: 200px;
    background-color: #ccff99a1;
  }

  .box_three {
    position: absolute;
    top: 30%;
    right: 90%;
    height: 200px;
    width: 200px;
    background-color: #cccccc69;
  }

  .box_four {
    position: absolute;
    top: 50%;
    right: 60%;
    height: 200px;
    width: 200px;
    background-color: #9752e769;
  }

  .login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 250px;
    background-color: #fff;
    border-radius: 3px;

    .login_form {
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
</style>